#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int n, k;
vector<int> opt[maxn], ps[maxn];
int tS[maxn];
int tot = 0;
int a[maxn], top = 0;
const int B = 103;
typedef unsigned long long ull;
ull gethas(vector<int> vv){
    ull hs = 0;
    for(auto t : vv){
	hs = 131ull * hs + (t + B);
    }
    return hs;
}
map<ull, int> mp;
void dfs(int st, int sum, int psum){
    if(sum <= k && st == n / k){
	++tot; opt[tot].push_back(0);
	for(int i = 1;i <= top;i++) opt[tot].push_back(a[i]);
	ps[psum].push_back(tot);
	tS[tot] = sum;
	opt[tot][0] = k - sum;
	mp[gethas(opt[tot])] = tot;
	return ;
    }
    for(int i = 0;i <= k - sum;i++){
	a[++top] = i; dfs(st + 1, sum + i, psum + i * (st + 1));
	top--;
    }
    return ;
}
const int maxV = 2e5 + 5;
ll f[maxV][105];
int G[maxV][105];
vector<int> tmp;
int pa[maxn];
inline void upd(ll &x, ll y){
    x = min(x, y); return ;
}
int main(){
    freopen("diyiti.in", "r", stdin);
    freopen("diyiti.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k; dfs(0, 0, 0);
    for(int i = 1;i <= n;i++) cin >> pa[i];
    sort(pa + 1, pa + n + 1);
    pa[0] = pa[n + 1] = -1;
    for(int i = 1;i <= tot;i++){
	tmp = opt[i];
	for(int j = 0;j < n / k;j++){
	    if(tmp[j] == 0) continue;
	    tmp[j]--; tmp[j + 1]++;
	    ull pid = gethas(tmp);
	    if(mp.find(pid) != mp.end()){
		G[i][j] = mp[gethas(tmp)];
	    }
	    // cerr << ": " << i << " " << j << " " << G[i][j] << endl;
	    tmp[j]++; tmp[j + 1]--;
	}
    }
    /*
    cerr << tot << endl;
    for(int i = 1;i <= tot;i++){
	for(auto t : opt[i]) cerr << t << " ";
	cerr << endl;
    }
    */
    memset(f, 0x3f, sizeof(f));
    f[1][n / k] = 0;
    for(int i = 0;i < n;i++){
	for(auto t : ps[i]){
	    for(int j = n / k;j >= 0;j--){
		// cerr << "?? " << t << " " << j << " " << f[t][j] << endl;
		if(j){
		    upd(f[t][j - 1], f[t][j]);
		}
		if(opt[t][j] && G[t][j]){
		    int num = (pa[i + 2] != pa[i + 1]) ? n / k : j;
		    // cerr << t << " " << j << " " << num << endl;
		    upd(f[G[t][j]][num], f[t][j] + (j == 0 ? -pa[i + 1] : 0) + (j == n / k - 1 ? pa[i + 1] : 0));
		    // cerr << t << " " << j << " " << G[t][j] << " " << num << " " <<
		    //	(j == 0 ? -pa[i + 1] : 0) + (j == n / k - 1 ? pa[i + 1] : 0) << endl;
		}
	    }
	}
    }
    ll ans = 0x3f3f3f3f3f3f3f3fll;
    for(auto t : ps[n]){
	// cerr << "? " << t << endl;
	for(int i = 0;i <= n / k;i++) upd(ans, f[t][i]);
    }
    if(ans == 0x3f3f3f3f3f3f3f3fll) ans = -1;
    cout << ans << endl;
    return 0;
}
